import java.util.Stack;

class Solution4 {
    public boolean backspaceCompare(String s, String t) {
        Stack<Character> stack1 = new Stack<>();
        Stack<Character> stack2 = new Stack<>();
        int lens = s.length();
        int lent = t.length();
        for(int i = 0; i < lens; i++ ) {
            if(s.charAt(i) == '#') {
                stack1.pop();
            } else {
                stack1.push(s.charAt(i));
            }
        }
        for(int i = 0; i < lent; i++ ) {
            if(t.charAt(i) == '#') {
                stack2.pop();
            } else {
                stack2.push(t.charAt(i));
            }
        }
        if(stack1.size() == stack2.size()) {
            while(!stack1.empty()) {
                if(stack1.pop() != stack2.pop()) {
                    return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }
}
